package com.gg.web;

import com.gg.pojo.Book;
import com.gg.pojo.Cart;
import com.gg.pojo.CartItem;
import com.gg.service.BookService;
import com.gg.service.impl.BookServiceImpl;
import com.gg.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CarServlet extends BaseServlet{

    private BookService bookService = new BookServiceImpl();

        /**
         * 加入购物车实现
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("加入购物车");
        System.out.println("商品编号:"+ req.getParameter("bookId"));

        //1.获取请求参数，商品编号
        int id = WebUtils.parserInt(req.getParameter("bookId"), 0);
        //2.调用queryBookById(id):Book得到图书信息
        Book book = bookService.queryBookById(id);
        //3.把图书信息，转换成CartItem商品项
        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //4.调用Cart.addItem(CartItem) 添加商品项到购物车
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null){
            //先判断有没有车，没有车才创建车，并放到Session
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
        System.out.println(cart);
        //5.重定向原来商品所在的页面
        resp.sendRedirect(req.getHeader("Referer"));
        //最后一个添加商品的名称保存起来，用于首页的展示
        req.getSession().setAttribute("lastItemName",cartItem.getName());
    }

    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数，商品编号
        int id = WebUtils.parserInt(req.getParameter("bookId"), 0);
        //2.获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //3.Cart.addItem(id)
        if (cart!=null){
            cart.deleteItem(id);
            //4.重定向原来商品所在的页面
            resp.sendRedirect(req.getHeader("Referer"));
        }

    }

    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //2.Cart.clear()
        if (cart!=null){
            cart.clear();
            //4.重定向原来商品所在的页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求参数，商品编号，数量
        int id = WebUtils.parserInt(req.getParameter("bookId"), 0);
        int count = WebUtils.parserInt(req.getParameter("count"), 1);
        //2.获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        //2.Cart.clear()
        if (cart!=null){
            cart.updateCount(id,count);
            //4.重定向原来商品所在的页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
}

